export function merge(intervals: number[][]): number[][] {
    if (!intervals || intervals.length < 2) return intervals;

    const sorted = intervals.sort((a, b) => a[0] - b[0]);
    const merged: number[][] = [];

    const len = sorted.length;
    for (let i = 0; i < len; i++) {
        const [left, right] = sorted[i];
        if (merged.length === 0) { 
            // 如果 merged 为空数组，直接添加
            merged.push([left, right]);
        } else if (merged[merged.length-1][1] >= left) { 
            // merged 数组最后一个元素的右边界 >= 当前左边界
            // 更新 右边界
            const oldRight = merged[merged.length - 1][1];
            merged[merged.length - 1][1] = Math.max(oldRight, right);
        } else {
            // 没有重叠，则添加一个区域
            merged.push([left, right]);
        }
    }
    return merged;
};